package com.bayair.doctor.ui.inquiry_record;


import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.bayair.doctor.R;
import com.blankj.utilcode.util.ColorUtils;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.tencent.qcloud.tim.uikit.component.UnreadCountTextView;
import com.tencent.qcloud.tim.uikit.modules.conversation.LabelLinearLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationIconView;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.utils.DateTimeUtil;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;

import java.util.Date;


/**
 会话 Item：普通会话
 */
public class CommonProvider extends BaseItemProvider<ConversationInfo, BaseViewHolder> {

    @Override
    public int viewType() {
        return ConversationInfo.TYPE_COMMON;
    }

    @Override
    public int layout() {
        return R.layout.item_conversation_common;
    }

    @Override
    public void convert(BaseViewHolder helper, ConversationInfo conversation, int position) {

        LabelLinearLayout itemLayout = helper.getView(R.id.item_left);
        TextView titleText = helper.getView(R.id.conversation_title);
        TextView messageText = helper.getView(R.id.conversation_last_msg);
        TextView timelineText = helper.getView(R.id.conversation_time);
        UnreadCountTextView unreadText = helper.getView(R.id.conversation_unread);
        ConversationIconView conversationIconView = helper.getView(R.id.conversation_icon);

        int itemColorBg = conversation.isTop() ? ColorUtils.getColor(R.color.conversation_top_color) : Color.WHITE;
        //会话item背景
        itemLayout.setBackgroundColor(itemColorBg);
        //服务中标签
        itemLayout.setLabelVisual(conversation.isInServer());
        //会话标题
        titleText.setText(conversation.getTitle());

        //会话最后一条消息（时间和内容）
        MessageInfo lastMsg = conversation.getLastMessage();
        if (lastMsg != null && lastMsg.getStatus() == MessageInfo.MSG_STATUS_REVOKE) {
            if (lastMsg.isSelf()) {
                lastMsg.setExtra("您撤回了一条消息");
            } else if (lastMsg.isGroup()) {
                String message = TUIKitConstants.covert2HTMLString(
                        TextUtils.isEmpty(lastMsg.getGroupNameCard()) ? lastMsg.getFromUser() : lastMsg.getGroupNameCard());
                lastMsg.setExtra(message + "撤回了一条消息");
            } else {
                lastMsg.setExtra("对方撤回了一条消息");
            }
        }
        messageText.setText("");
        timelineText.setText("");
        if (lastMsg != null) {
            if (lastMsg.getExtra() != null) {
                messageText.setText(Html.fromHtml(lastMsg.getExtra().toString()));
                messageText.setTextColor(ColorUtils.getColor(R.color.list_bottom_text_bg));
            }
            timelineText.setText(DateTimeUtil.getTimeFormatText(new Date(lastMsg.getMsgTime() * 1000)));
        }

        //会话未读计算
        if (conversation.getUnRead() > 0) {
            unreadText.setVisibility(View.VISIBLE);
            if (conversation.getUnRead() > 99) {
                unreadText.setText("99+");
            } else {
                unreadText.setText("" + conversation.getUnRead());
            }
        } else {
            unreadText.setVisibility(View.GONE);
        }

        //会话头像
        if (conversation.getIconUrlList() != null) {
            conversationIconView.setConversation(conversation);
        }
    }
}
